package Day08_19;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class HomeWork_CopyCatalogue {
    public static void main(String[] args) {
        File srcFile = new File("C:\\DD\\JavaProjects\\modul");
        File destFile = new File("D:");
        copy(srcFile,destFile);

    }
    public static void copy(File srcFile,File destFile){
        if (srcFile.isFile()){
            String s1 = srcFile.getAbsolutePath();
            String s2 = destFile.getAbsolutePath().substring(0,2) + s1.substring(2);
          //  destFile = new File(s2);
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
             //   destFile.createNewFile();这里在只是 目录 文件框架拷贝进去D盘了，内容却没有！！！
                fis = new FileInputStream(srcFile);
                fos = new FileOutputStream(s2);
                byte[] bytes = new byte[1024*1024];
                int readCount = 0;
                while ((readCount = fis.read(bytes))!=-1){
                        fos.write(bytes,0,readCount);//读多少-----写多少
                }
                fos.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                if(fis!=null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            return ;
        }
        File[] files = srcFile.listFiles();
        for (File i:files){
            if(i.isDirectory()){
                String s1 = i.getAbsolutePath();
                String s2 = destFile.getAbsolutePath().substring(0,2) + s1.substring(2);
                destFile = new File(s2);
                if(!destFile.exists()) {
                    destFile.mkdirs();
                }
            }
            copy(i,destFile);
        }
    }

}
